<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" :width="700" :defaultFullscreen="true">
    <ul class="desc">
      <li>提醒：</li>
      <li>1.请勿修改脚本内容中的方法名，否则可能将导致测试失败</li>
      <li>2.切换脚本类型时，将会重置脚本内容，导致修改后的脚本内容丢失，请谨慎操作</li>
      <li>3.只有测试通过了，才可以保存脚本</li>
    </ul>
    <BasicForm @register="registerForm" />
    <div v-show="isShowFooter"><span class="result">测试结果:</span>{{ result }}</div>
    <template #footer>
      <a-button type="primary" v-show="isShowFooter" @click="handleCheck">校验</a-button>
      <a-button type="primary" v-show="isShowFooter" :disabled="result !== '校验成功'" @click="handleSubmit">确认</a-button>
      <a-button @click="handleCancel">取消</a-button>
    </template>
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { formSchema } from './script.data';
  import { check, saveOrUpdateScript } from './script.api';
  const { createMessage: $message } = useMessage();
  // 声明Emits
  const emit = defineEmits(['success', 'register']);
  const isUpdate = ref(true);
  const isShowFooter = ref(true);
  const result = ref('');
  //表单配置
  const [registerForm, { resetFields, setFieldsValue, validate, setProps }] = useForm({
    labelWidth: 150,
    schemas: formSchema,
    showActionButtonGroup: false,
  });
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单
    await resetFields();
    isUpdate.value = !!data?.isUpdate;
    isShowFooter.value = !!data?.showFooter;
    setModalProps({ confirmLoading: false });
    if (unref(isUpdate)) {
      //表单赋值
      await setFieldsValue({
        ...data.record,
      });
    }
    setProps({ disabled: !data?.showFooter });
  });
  //设置标题
  const getTitle = computed(() => (!unref(isUpdate) ? '新增脚本' : '编辑脚本'));
  //表单提交事件
  async function handleSubmit() {
    try {
      const values = await validate();
      setModalProps({ confirmLoading: true });
      //提交表单
      await saveOrUpdateScript(values, isUpdate.value);
      //关闭弹窗
      closeModal();
      //刷新列表
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
  async function handleCheck() {
    const values = await validate();
    let params: any = {};
    params.script = values.script;
    params.scriptType = values.scriptType;
    const { success, msg } = await check(params);
    if (success) {
      result.value = '校验成功';
      $message.success('校验成功');
      handleCancel();
    } else {
      result.value = '校验失败';
      $message.error(msg);
    }
  }
  function handleCancel() {
    resetFields();
    closeModal();
  }
</script>
<style scoped>
  .desc {
    color: #e6a23c;
    line-height: 20px;
    font-size: 14px;
    margin: 0 0 16px 90px;
  }
  .result {
    display: inline-block;
    width: 150px;
    text-align: right;
  }
</style>
